<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\User;

/**
 * UserSearch represents the model behind the search form about `app\models\User`.
 */
class UserSearch extends User
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'loginTime', 'createdTime'], 'integer'],
            [['email', 'password', 'roles', 'truename', 'idcard', 'gender', 'birthday', 'city', 'mobile', 'qq', 'company', 'job', 'class', 'student_id', 'weixin', 'iam', 'loginIp', 'createdIp'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = User::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,'pagination' => ['pagesize' => '10'],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'birthday' => $this->birthday,
            'loginTime' => $this->loginTime,
            'createdTime' => $this->createdTime,
        ]);

        $query->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'password', $this->password])
            ->andFilterWhere(['like', 'roles', $this->roles])
            ->andFilterWhere(['like', 'truename', $this->truename])
            ->andFilterWhere(['like', 'idcard', $this->idcard])
            ->andFilterWhere(['like', 'gender', $this->gender])
            ->andFilterWhere(['like', 'city', $this->city])
            ->andFilterWhere(['like', 'mobile', $this->mobile])
            ->andFilterWhere(['like', 'qq', $this->qq])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'job', $this->job])
            ->andFilterWhere(['like', 'class', $this->class])
            ->andFilterWhere(['like', 'student_id', $this->student_id])
            ->andFilterWhere(['like', 'weixin', $this->weixin])
            ->andFilterWhere(['like', 'iam', $this->iam])
            ->andFilterWhere(['like', 'loginIp', $this->loginIp])
            ->andFilterWhere(['like', 'createdIp', $this->createdIp]);

        return $dataProvider;
    }

    /**
     *获取班级ID
     */
    public static function getClassId(){
        $classIds = array();

        $sql = "select DISTINCT class from user";

        $classDate = Yii::$app->db->createCommand($sql)->queryAll();


        if (!empty($classDate)) {
            foreach ($classDate as $class) {
                $classIds[$class['class']] = $class['class'] . "班";
            }
        }
        return $classIds;
    }
}
